/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.ad.service;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.boxin.ims.modules.ad.entity.Advertisement;
import com.boxin.ims.modules.ad.dao.AdvertisementDao;
import com.boxin.ims.modules.wechat.entity.LotteryRecord;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.LotteryRecordService;

/**
 * 广告Service
 * @author Jakemanse
 * @version 2014-04-18
 */
@Component
@Transactional(readOnly = true)
public class AdvertisementService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(AdvertisementService.class);
	
	@Autowired
	private AdvertisementDao advertisementDao;
	

	@Autowired
	private LotteryRecordService lotteryRecordService;
	
	
	public Advertisement get(Long id) {
		return advertisementDao.findOne(id);
	}
	
	
	
//	public void clear(){
//		advertisementDao.clear();
//	}
	
	
	public Page<Advertisement> find(Page<Advertisement> page, Advertisement advertisement) {
		DetachedCriteria dc = advertisementDao.createDetachedCriteria();
		if (advertisement.getUser()!=null && advertisement.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", advertisement.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(advertisement.getName())){
			dc.add(Restrictions.like("name", "%"+advertisement.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return advertisementDao.find(page, dc);
	}
	
	
	
	
	
	@Transactional(readOnly = false)
	public void save(Advertisement advertisement) {
		advertisementDao.clear();
		
		advertisement.setContent(StringEscapeUtils.unescapeHtml4(advertisement.getContent()));
		advertisementDao.save(advertisement);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		advertisementDao.deleteById(id);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-4-21  上午10:40:37
	 * @function <p>  </p>
	 * @param userId
	 * @return
	 */
	public List<Advertisement> findSellerAdList(Long userId){
		
		return advertisementDao.findSellerAdList(userId);
		
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-4-22  下午12:01:40
	 * @function <p> 广告主的   广告 </p>
	 * @param userId
	 * @return
	 */
	public List<Advertisement> findMyAdvertisement(Long userId){
		return advertisementDao.findMyAdvertisement(userId);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-4-22  上午9:54:05
	 * @function <p>查找我的广告奖品</p>
	 * @param user
	 * @param member
	 * @return
	 */
	public  List<LotteryRecord> findMyAdvertisementLotteryList(User user,WechatMember member){
		
		
		WechatMember queryMember = new WechatMember();
		queryMember.setMobile(member.getMobile());
		
		LotteryRecord lotteryRecord = new LotteryRecord();
		lotteryRecord.setSourceType(LotteryRecord.SURCE_TYPE_ADVERT);
		lotteryRecord.setUser(user);
		lotteryRecord.setMember(queryMember);
		lotteryRecord.setStatus(LotteryRecord.STATUS_NORMAL);
		
		List<LotteryRecord>   lotteryList = lotteryRecordService.find( lotteryRecord);
		
		return lotteryList;
	}
	
	public void clear(){
		advertisementDao.clear();
	}
	
}
